package com.mine.dubbo.netty.client;

import com.mine.dubbo.common.MyConstant;
import com.mine.dubbo.netty.DefaultFuture;
import com.mine.dubbo.netty.model.Request;
import com.mine.dubbo.netty.model.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        Response response = (Response) msg;
        if (response.getType() == MyConstant.TYPE_PING) {
            System.out.println("ping");
            return;
        }
        log.info("收到消息，类型为{}", response.getType());
        DefaultFuture.receive(response);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            switch (event.state()) {
                case ALL_IDLE:
                    Request request = new Request(1, "ping");
                    ctx.channel().writeAndFlush(request);
                default:
            }
        }
    }
}
